package cn.ghf.org.controller;

import cn.ghf.basic.exception.MyException;
import cn.ghf.basic.util.AjaxResult;
import cn.ghf.basic.util.PageList;
import cn.ghf.org.domain.Shop;
import cn.ghf.org.query.ShopQuery;
import cn.ghf.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class ShopController {

    @Autowired
    private IShopService service;

    /**
     * 根据id查询
     * @return
     */
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id){
        return service.loadById(id);
    }
    /**
     * 查询所有
     */
    @GetMapping
    public List<Shop> queryAll(){
        return service.loadAll();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.createAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setSuccess(false).setMsg("c操作失败");
        }
    }

    /**
     * 添加或修改操作
     */
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){
                service.add(shop);
            }else{
                service.update(shop);
            }
            return AjaxResult.createAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setSuccess(false).setMsg("操作失败");
        }
    }

    /**
     * 分页
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return service.queryData(shopQuery);
    }
    /**
     * 批量删除
     */
    @PatchMapping("/patch")
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            service.patchDelete(ids);
            return AjaxResult.createAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setSuccess(false).setMsg("删除失败");
        }

    }

    @PostMapping("/settlement")
    public AjaxResult setTlement(@RequestBody Shop shop){
        try {
            service.setTlement(shop);
            return AjaxResult.createAjaxResult();
        } catch (MyException e) {
            e.printStackTrace();
            return  AjaxResult.createAjaxResult().setMsg(e.getMessage());
        }
    }
}
